//
//  QYBorrowedRecordCell.m
//  QingYue
//
//  Created by Zheng on 2017/6/14.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYBorrowedRecordCell.h"

#import "QYBorrowedRecordModel.h"

@implementation QYBorrowedRecordCell

- (void)buttonClickAction:(UIButton *)sender {
    if (sender == _renewBtn) {
        NSLog(@"续借");
        _renewBlock(@(_model.properthID.integerValue));
    } else {
        NSLog(@"归还");
        _returnBlock(@(_model.Isbn.integerValue));
    }
}


- (void)setModel:(QYBorrowedRecordModel *)model {
    _model = model;
    
    _titleLbl.text = _model.bookName;
    
    if (_quickBuilder) {
        _quickBuilder = nil;
    }
    
    if (_formView) {
        [_formView removeFromSuperview];
        _formView = nil;
    }
    
    _quickBuilder = [[KWFormViewQuickBuilder alloc] init];
    [_quickBuilder addRecord:@[@"条码号",_model.properthID]];
    [_quickBuilder addRecord:@[@"借阅日期",_model.borrowTime]];
    [_quickBuilder addRecord:@[@"到期日期",_model.BorrowEndTime]];
    
    _formView = [_quickBuilder startCreatWithWidths:@[@((MSSize_Width-40)/3),@((MSSize_Width-40)/3*2)] startPoint:CGPointMake(20, 45)];
    
    [self.contentView addSubview:_formView];
    
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.font = Font(17);
        
    }
    return _titleLbl;
}

- (UIButton *)renewBtn {
    if (!_renewBtn) {
        _renewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_renewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _renewBtn.titleLabel.font = Font(14);
        _renewBtn.layer.backgroundColor = RGB(232, 232, 232, 1).CGColor;
        _renewBtn.layer.cornerRadius = 3;
        _renewBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _renewBtn.layer.borderWidth = 1;
        _renewBtn.layer.masksToBounds = YES;
        [_renewBtn setTitle:@"续借" forState:UIControlStateNormal];
        [_renewBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _renewBtn;
}

- (UIButton *)returnBtn {
    if (!_returnBtn) {
        _returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_returnBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _returnBtn.titleLabel.font = Font(14);
        _returnBtn.layer.backgroundColor = RGB(232, 232, 232, 1).CGColor;
        _returnBtn.layer.cornerRadius = 3;
        _returnBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _returnBtn.layer.borderWidth = 1;
        _returnBtn.layer.masksToBounds = YES;
        [_returnBtn setTitle:@"归还" forState:UIControlStateNormal];
        [_returnBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnBtn;
}


- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, 24, 239);
    CGContextAddLineToPoint(context, MSSize_Width-24, 239);
    CGContextSetLineDash(context, 0, (CGFloat[]){3,1}, 2);
    CGContextDrawPath(context, kCGPathStroke);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.titleLbl];
        [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.top.equalTo(self).offset(5);
            make.height.mas_equalTo(40);
        }];
        
        [self.contentView addSubview:self.returnBtn];
        [_returnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@20);
            make.bottom.equalTo(@(-20));
            make.height.equalTo(@35);
            make.width.equalTo(@105);
        }];
        
        [self.contentView addSubview:self.renewBtn];
        [_renewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_returnBtn.mas_right).offset(30);
            make.bottom.height.width.equalTo(_returnBtn);
        }];
        
    }
    return self;
}

@end
